﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Interfaces;
using Cofe.Core.Property;

namespace Cofe.Core.EntryType
{
    public static class IEntryTypeInfoManagerExtension
    {
        public static Task<IPropertyProvider> GetEntryTypeInfoPPAsync(this IEntryTypeInfoManager manager, string rootKey, IPropertyHost propertyHost)
        {
            return manager.GetEntryTypeInfoPPAsync(rootKey, manager.GetEntryTypeKey(propertyHost));
        }

        public static async Task<IEntryTypeInfo> GetEntryTypeInfoAsync(this IEntryTypeInfoManager manager, string rootKey, string entryTypeKey, 
            IEntryConstructor entryConstructor = null)
        {
            var ec = entryConstructor ?? CofeServices.ServiceLocater.FindService<IEntryConstructor>();
            return await ec.ConstructEntryAsync<IEntryTypeInfo>(ec.ConstructPropertyHost(
                await manager.GetEntryTypeInfoPPAsync(rootKey, entryTypeKey)));
        }

        public static async Task<IEntryTypeInfo> GetEntryTypeInfoAsync(this IEntryTypeInfoManager manager, string rootKey, IPropertyHost propertyHost,
           IEntryConstructor entryConstructor = null)
        {
            string key = manager.GetEntryTypeKey(propertyHost);
            if (key == null)
                return null;
            return await manager.GetEntryTypeInfoAsync(rootKey, manager.GetEntryTypeKey(propertyHost));
        }
    }
}
